package com.horoscope.astrology.zodiac.signs.free.utils

import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Rect
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.view.inputmethod.InputMethodManager
import com.horoscope.astrology.zodiac.signs.free.MmoApplication
import java.lang.reflect.Method


/**
 *  作者    1024
 *  时间    2020/3/4 上午9:22
 *  文件    constellation-android
 *  描述    导航栏设置
 */
object NavigationBarUtil {

    fun initActivity(content: View) {
        NavigationBarUtil(content)
    }

    /**
     * 被监听的视图
     */
    private var mObserved: View? = null
    /**
     * 视图变化前的可用高度
     */
    private var usableHeightView = 0
    private var layoutParams: ViewGroup.LayoutParams? = null

    private fun NavigationBarUtil(content: View) {
        mObserved = content
        //给View添加全局的布局监听器监听视图的变化
        mObserved!!.viewTreeObserver.addOnGlobalLayoutListener(OnGlobalLayoutListener { resetViewHeight1() })
        layoutParams = mObserved!!.layoutParams
    }


    private var usableHeight = 0
    private fun resetViewHeight1() {
        val usableHeightViewNow = CalculateAvailableHeight()
        //比较布局变化前后的View的可用高度
        val inputMethodManager: InputMethodManager =
            MmoApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        val rect = Rect()
        mObserved!!.getWindowVisibleDisplayFrame(rect)
        usableHeight = Math.max(usableHeight, rect.bottom)
        if (inputMethodManager.isActive() && usableHeight > rect.bottom) { //软键盘显示，导致界面布局改变
            return
        }
        if (usableHeightViewNow != usableHeightView) { //如果两次高度不一致
            //将当前的View的可用高度设置成View的实际高度
            val mConfiguration: Configuration =
                MmoApplication.getInstance().getResources().getConfiguration() //获取设置的配置信息
            val ori: Int = mConfiguration.orientation //获取屏幕方向
            if (ori == Configuration.ORIENTATION_LANDSCAPE) { //横屏
                layoutParams!!.width = usableHeightViewNow
            } else if (ori == Configuration.ORIENTATION_PORTRAIT) { //竖屏
                layoutParams!!.height = usableHeightViewNow
            }
            mObserved!!.requestLayout() //请求重新布局
            usableHeightView = usableHeightViewNow
        }
    }

    /**
     * 计算试图高度
     *
     * @return
     */
    private fun CalculateAvailableHeight(): Int {
        val r = Rect()
        mObserved!!.getWindowVisibleDisplayFrame(r)
        val mConfiguration: Configuration =
            MmoApplication.getInstance().getResources().getConfiguration() //获取设置的配置信息
        val ori: Int = mConfiguration.orientation //获取屏幕方向
        if (ori == Configuration.ORIENTATION_LANDSCAPE) { //横屏
            return r.right
        } else if (ori == Configuration.ORIENTATION_PORTRAIT) { //竖屏
            return r.bottom
        }
        //        return (r.bottom - r.top);//如果不是沉浸状态栏，需要减去顶部高度
        return r.bottom //如果是沉浸状态栏
    }

    /**
     * 判断底部是否有虚拟键
     *
     * @param context
     * @return
     */
    fun hasNavigationBar(context: Context): Boolean {
        var hasNavigationBar = false
        val rs: Resources = context.resources
        val id: Int = rs.getIdentifier("config_showNavigationBar", "bool", "android")
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id)
        }
        try {
            val systemPropertiesClass =
                Class.forName("android.os.SystemProperties")
            val m: Method = systemPropertiesClass.getMethod("get", String::class.java)
            val navBarOverride =
                m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String
            if ("1" == navBarOverride) {
                hasNavigationBar = false
            } else if ("0" == navBarOverride) {
                hasNavigationBar = true
            }
        } catch (e: Exception) {

        }
        return hasNavigationBar
    }
}